<?php
class EditAdminNoticeForm extends Form{

	function EditAdminNoticeForm(){		
		Form::Form('EditAdminNoticeForm');
		CGlobal::$website_title="Gửi thông báo cho tất cả thành viên";		
		$this->link_css('style/enbacStyle.css');								
	}
	
	function on_submit(){
	
		$title = AZLib::getParam('title');
		$sapo = AZLib::getParam('sapo');
		$link = AZLib::getParam('link','');
		$active = (int)Url::get('active');
		$id = (int)Url::get('id');
		$cmd = Url::get('cmd');
		
		if(!$title || !$sapo){
			$this->setFormError('title','Dữ liệu không hợp lệ!'); 
		}
		else{				
			if($cmd=="add"){
				$item_array = array(
							'title' => $title,
							'sapo' => $sapo,
							'link' => $link,
							'admin_add' => User::user_name(),
							'active' => $active,
							'time_add' => TIME_NOW
					);
					
				DB::insert('admin_notice',	$item_array);
			}
			elseif($cmd=="edit" && $id){
				$item_array = array(
							'title' => $title,
							'sapo' => $sapo,
							'link' => $link,
							'admin_edit' => User::user_name(),
							'active' => $active,
							'time_edit' => TIME_NOW
					);
				DB::update_id('admin_notice', $item_array,$id);	
			}
			User::getAdminNotice(0,1);
			Url::redirect_current();
		}
		
	}
	
	function draw(){	
		$this->beginForm();		
		global $display;		
				
		$id  = (int)Url::get("id",0);
		$cmd = Url::get("cmd");
		
		if($id && $cmd=="edit"){
			$item = DB::fetch("SELECT * FROM admin_notice WHERE id=$id");
			$display->add('title', $item['title']);
			$display->add('sapo', $item['sapo']);
			$display->add('link', $item['link']);
			$display->add('active', $item['active']);
		}
		elseif($cmd=="add"){
			$display->add('title', Url::get('title'));
			$display->add('sapo', Url::get('sapo'));
			$display->add('link', Url::get('link'));
			$display->add('active', (int)Url::get('active',1));
		}
		$display->add('cmd', $cmd);
		$display->add('msg',$this->showFormErrorMessages(1));
		
		$display->output('EditAdminNotice');		
		$this->endForm();
	}
}
?>